
package com.jl.tools.stkeditor.manager;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author LuHongjun
 */
public class LineInfo {

	private String group;
	private String codeScheam;
	private String with80;
	private String tag;
	private String text;
	private boolean isOK = false;
	private int index = 0;
	private String name;

	public LineInfo(String oneLineAllText) {
		String reg = "^\\[G:(.*)/C:(.*)/80:(.*)/T:(.*)/N:(.*)\\](.*)$";
		Pattern pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(oneLineAllText);
		if (matcher.find()) {
			isOK = true;
			group = matcher.group(1);
			codeScheam = matcher.group(2);
			with80 = matcher.group(3);
			tag = matcher.group(4);
			name = matcher.group(5);
			text = matcher.group(6);
		} else {
			isOK = false;
		}
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public int getGroup() {
		if (!group.matches("\\d{1,6}") || group == null || group.trim().equals("") || group.equalsIgnoreCase("none")) {
			return -1;
		} else {
			return Integer.parseInt(group);
		}
	}

	public CodingType getCodeScheame() {
		if (codeScheam.equalsIgnoreCase("Original") || codeScheam.equalsIgnoreCase("none")) {
			return CodingType.ORIGINAL;
		} else if (codeScheam.equalsIgnoreCase("ASCII")) {
			return CodingType.ASCII;
		} else if (codeScheam.equalsIgnoreCase("7Bit") || codeScheam.equalsIgnoreCase("Bit7")) {
			return CodingType.BIT7;
		} else if (codeScheam.equalsIgnoreCase("UCS2")) {
			return CodingType.UCS2;
		}
		return CodingType.UNICODE;
	}

	public boolean isWith80() {
		if (with80.equalsIgnoreCase("false")) {
			return false;
		}
		return true;
	}

	public String getTag() {
		return tag;
	}

	/**
	 * check this line is match the pattern or not
	 * 
	 * @return
	 */
	public boolean isOK() {
		return isOK;
	}

	public String getText() {
		return text;
	}
	
	public String getName(){
		return name;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(text).append("[G:").append(group).append("/").append("C:").append(codeScheam).append("/").append("80:").append(with80).append("/").append("T:").append(tag).append("/N:").append(name).append("]");
		return sb.toString();
	}
}
